package com.bskj.bms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bskj.bms.bean.req.BillListReqVo;
import com.bskj.bms.bean.vo.AllParkinfoVo;
import com.bskj.bms.bean.vo.PropCustRespVo;
import com.bskj.bms.cache.PropertyCustCache;
import com.bskj.bms.service.IBmsIncomeService;
import com.bskj.bms.service.IBmsPropCustService;
import com.bskj.common.bean.model.AggregateAmountsVo;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.dao.core.Page;

@RequestMapping("/bill")
@Controller
public class BillController extends BaseController{
	@Autowired
	private IBmsIncomeService bmsIncomeService;
	
	@Autowired
	private PropertyCustCache propertyCustCache;
	
	@Autowired
	private IBmsPropCustService bmsPropCustService;
	
	@RequestMapping("/index")
	public ModelAndView index() throws Exception{
		ModelAndView mv=new ModelAndView();
		List<String> boxList = bmsIncomeService.getBoxNoList(getSessionUser().getCustId());
		if(getSessionUser().getType() == 4){
			List<ParkingLotCustomer> parkingLotCustomers=propertyCustCache.getAreaName(getSessionUser().getPropId());
			mv.addObject("parkingLotCustomers", parkingLotCustomers);
		}
		mv.setViewName("/bill/index");
		mv.addObject("boxList", boxList);
		return mv;
	}
	
	@RequestMapping("/propIncome")
	public ModelAndView propIncome() throws Exception{
		ModelAndView mv=new ModelAndView();
		if(getSessionUser().getType() == 4){
			List<ParkingLotCustomer> parkingLotCustomers=propertyCustCache.getAreaName(getSessionUser().getPropId());
			mv.addObject("parkingLotCustomers", parkingLotCustomers);
		}
		mv.setViewName("/bill/propIncome");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("propList")
	public Map<String, Object> propList(BillListReqVo req) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		if(req.getCustId() == 0 ){
			req.setCustId(getSessionUser().getCustId());
		}
		req.setPropId(getSessionUser().getPropId());
		Page<PropCustRespVo> parkInfoPage =bmsPropCustService.getPropCustList(req);
		resultMap.put("data", parkInfoPage.getList());
		resultMap.put("draw", req.getDraw());
		resultMap.put("recordsTotal", parkInfoPage.getList().size());
		resultMap.put("recordsFiltered", parkInfoPage.getTotalRow());
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("inList")
	public Map<String, Object> indexList(BillListReqVo req) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		if(req.getCustId() == 0 ){
			req.setCustId(getSessionUser().getCustId());
		}
		req.setPropId(getSessionUser().getPropId());
		List<AggregateAmountsVo> list=bmsIncomeService.FindAggregateAmount(req);
		resultMap.put("aggregateAmounts", list);
		return resultMap;
	}
	
	@RequestMapping("/income")
	public ModelAndView income(Integer custId) throws Exception{
		ModelAndView mv=new ModelAndView();
		List<String> boxList=new ArrayList<>();
		if(custId == null || custId == 0){
			boxList = bmsIncomeService.getBoxNoList(getSessionUser().getCustId());
		}else {
			boxList = bmsIncomeService.getBoxNoList(custId);
		}
		mv.setViewName("/bill/income");
		mv.addObject("boxList", boxList);
		mv.addObject("custId", custId);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("list")
	public Map<String, Object> list(BillListReqVo req) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		if(req.getCustId() == 0 ){
			req.setCustId(getSessionUser().getCustId());
		}
		Page<AllParkinfoVo> parkInfoPage =bmsIncomeService .pageIncome(req);
		resultMap.put("data", parkInfoPage.getList());
		resultMap.put("draw", req.getDraw());
		resultMap.put("recordsTotal", parkInfoPage.getList().size());
		resultMap.put("recordsFiltered", parkInfoPage.getTotalRow());
		return resultMap;
	}
}
